﻿using System.Collections;
using System.Data.SqlClient;
using System.Diagnostics;

namespace ServerAnalysisReport
{
    class DataBase
    {
        private readonly ArrayList targetservers;
        private readonly Queue servernamesQueue;
        private readonly bool cancelRunQueueJob = false;

        public DataBase(ArrayList servernames)
        {
            targetservers = servernames;
            servernamesQueue = new Queue(targetservers.Count);
            foreach (string servername in targetservers)
            {
                addservernameQueue(servername);
            }
            
        }
        public void addservernameQueue(string servername)
        {
            servernamesQueue.Enqueue(servername);
        }


        public int servernamesQueueCount()
        {
            return servernamesQueue.Count;
        }

        public ArrayList RunAllQueueJob(int[] targetmetricid, string datemin, string datemax, int timeout)
        {

            ArrayList returnJobs = new ArrayList();
            for (int counter = 0; counter < servernamesQueue.Count; counter++)
            {
                if ((servernamesQueue.Count > 0) && (cancelRunQueueJob == false))
                {
                    returnJobs.Add(GetDBData(targetmetricid, (string)servernamesQueue.Dequeue(), datemin, datemax, timeout));
                }
            }
            return returnJobs;
        }

        public ArrayList RunSingleQueueJob(int[] targetmetricid, string datemin, string datemax, int timeout)
        {
            if (servernamesQueue.Count > 0)
            {
                return GetDBData(targetmetricid, (string)servernamesQueue.Dequeue(), datemin, datemax, timeout);
            }
            else
            {
                ArrayList temp = new ArrayList();
                temp.Add("No More jobs availale");
                return temp;
            }

            
        }

        private static ArrayList GetDBData(int[] targetmetricid, string targetservername, string datemin, string datemax, int timeout)
        {

            SqlConnection conn = new SqlConnection(ServerAnalysisReport.Properties.Settings.Default.RMSConnectionString);

            ArrayList TotalServerStats = new ArrayList();
            TotalServerStats.Add(targetservername);
            ArrayList SingleMetricStats = new ArrayList();
            SqlCommand cmd = new SqlCommand();
            cmd.Connection = conn;
            for (int counter = 0; counter < targetmetricid.Length;counter++ )
            {

                SingleMetricStats.Add(targetmetricid[counter]);
                //SingleMetricStats.Add(targetservername);
                cmd.CommandText =

                @"select SDB_METRICS.METRICUPDATETIME,
                SDB_METRICS.SERVERUTCBIAS,
                SDB_METRICS.METRICSAMPLEPERIOD,
                SDB_METRICS.MINMETRICVALUE,
                SDB_METRICS.MAXMETRICVALUE,
                SDB_METRICS.MEANMETRICVALUE,
                SDB_METRICS.STDDEVMETRICVALUE
                from  SDB_METRICS, LU_METRIC, LU_METRICCOUNTER , LU_SERVER, LU_SERVERNAME
                where 
                SDB_METRICS.FK_METRICID = LU_METRIC.PK_METRICID
                and
                LU_METRIC.FK_METRICCOUNTERID = LU_METRICCOUNTER.PK_METRICCOUNTERID
                and 
                LU_METRICCOUNTER.PK_METRICCOUNTERID = " + targetmetricid[counter] + @"
			    and
			    SDB_METRICS.FK_SERVERID = LU_SERVER.PK_SERVERID
			    and
			    LU_SERVER.FK_SERVERNAMEID = LU_SERVERNAME.PK_SERVERNAMEID
			    and
			    SERVERNAME = '" + targetservername + @"'
                --and
                --FK_SERVERID = 1
                and METRICUPDATETIME > '" + datemin + @"'
                and METRICUPDATETIME < '" + datemax + @"'
                order by METRICUPDATETIME";
                cmd.CommandTimeout = timeout;

                conn.Open();
                SqlDataReader r = cmd.ExecuteReader();
                while (r.Read())
                {
                    string[] stringarray = new string[7];


                    stringarray[0] = r["METRICUPDATETIME"].ToString();
                    stringarray[1] = r["SERVERUTCBIAS"].ToString();
                    stringarray[2] = r["METRICSAMPLEPERIOD"].ToString();
                    stringarray[3] = r["MINMETRICVALUE"].ToString();
                    stringarray[4] = r["MAXMETRICVALUE"].ToString();
                    stringarray[5] = r["MEANMETRICVALUE"].ToString();
                    stringarray[6] = r["STDDEVMETRICVALUE"].ToString();
                    //stringarray[7] = targetmetricid[counter].ToString();
                    //stringarray[8] = targetservername;
                    SingleMetricStats.Add(stringarray);
                }
#if DEBUG
                Debug.WriteLine("Single Metric Stats Count 1 " + SingleMetricStats.Count);
#endif

                conn.Close();
                TotalServerStats.Add(SingleMetricStats.Clone());
                SingleMetricStats.Clear();

            }//end For

            
            return TotalServerStats;

        }




    }
}
